<template>
    <div class="update-category-from">
        <el-form :model="formData" status-icon :rules="rules" ref="formData" label-width="100px">
            <el-form-item label="类型名称：" prop="name">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script type="text/ecmascript-6">
    export default {
        props: {
            formData: {
                type: Object,
                name: Object
            }
        },
        data() {
            let validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('类型名称不能为空'));
                } else {
                    callback();
                }
            };
            let validateType = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('类型编号不能为空'));
                } else {
                    callback();
                }
            };
            return {
                rules: {
                    name: [
                        {validator: validateName, trigger: 'blur'}
                    ],
                    type: [
                        {validator: validateType, trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            submitForm() {
                this.$refs["formData"].validate((valid) => {
                    if (valid) {
                        this.$http.get("api/category/update", {
                            params:{
                                name: this.formData.name,
                                type: this.formData.type
                            }
                        }).then((response) => {
                            response = response.data;
                            if (response.code === 0) {
                                this.$message({
                                    message: response.msg,
                                    type: 'success',
                                    customClass: 'alert-shadow'
                                });
                            } else {
                                this.$message({
                                    message: response.msg,
                                    type: 'error',
                                    customClass: 'alert-shadow'
                                });
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            resetForm() {
                this.formData['name']='';
                this.formData['type']='';
                this.formData['parentType']='';
            }
        }
    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>

</style>
